package com.luli.Utils;

import com.luli.constant.Dict;
import com.luli.entity.UserData.UpdateUserParam;
import com.luli.entity.login.RegisterUser;
import com.luli.entity.response.ResponseMsg;
import com.luli.enumeration.MessageEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.luli.Utils.DateUtils.getAtPresent;
import static com.luli.Utils.ResponseMsgUtil.getFailMsg;
import static com.luli.Utils.ResponseMsgUtil.getSuccessMsg;

/**
 * 陆离
 * 2022/10/24 16:30:18
 * 校验 注册参数
 */
public class RegisterUtils {
    private static final String slat = "mszlu!@#";

    /**
     * 新增校验参数
     *
     * @param registerUser
     * @return
     */
    public static ResponseMsg<?> checkUser(RegisterUser registerUser) {
        RegisterUser userData = new RegisterUser();
        //检验用户名
        String userName = registerUser.getUserName();
        if (StringUtils.isBlank(userName)
                || userName.length() > 50) {
            return getFailMsg(MessageEnum.USERNAME_ISNULL.getCode(), MessageEnum.USERNAME_ISNULL.getMsg(), null);
        }
        userData.setUserName(userName);

        //校验账号 必须是11位 随机生成，并数据库没有该数据
        String snowflakeId = String.valueOf(SnowflakeIdWorker.getSnowflakeId());
        userData.setAccount(snowflakeId.substring(snowflakeId.length() - 13));

        //密码   长度不能超过30 不为空 最少6位 不能为汉字 可携带符号
        String passWord = registerUser.getPassWord();
        if (StringUtils.isBlank(passWord)) {
            return getFailMsg(MessageEnum.PASSWORD_ERROR.getCode(), MessageEnum.PASSWORD_ERROR.getMsg(), null);
        } else {
            Map<String, Object> checkPasswordMap = checkPassword(passWord);
            //密码加密 加盐
            passWord = DigestUtils.md5Hex(passWord + slat);
            userData.setPassWord(passWord);
            userData.setPassWordSecurity(String.valueOf(checkPasswordMap.get(Dict.MESSAGE)));
        }

        //地址
        String address = registerUser.getAddress();
        if (StringUtils.isNotEmpty(address)) {
            userData.setAddress(address);
        }

        //手机号 长度11位
        String phone = registerUser.getPhone();
        if (StringUtils.isBlank(phone)) {
            return getFailMsg(MessageEnum.PHONE_ERROR.getCode(), MessageEnum.PHONE_ERROR.getMsg(), null);
        }
        //正则校验格式
        if (!isMobileNO(phone)) {
            return getFailMsg(MessageEnum.PHONE_CODE_ERROR.getCode(), MessageEnum.PHONE_CODE_ERROR.getMsg(), null);
        } else {
            userData.setPhone(phone);
        }

        //性别    女生为0 男生为1
        String sex = registerUser.getSex();
        if (StringUtils.isBlank(sex)) {
            return getFailMsg(MessageEnum.SEX_ISNULL_ERROR.getCode(), MessageEnum.SEX_ISNULL_ERROR.getMsg(), null);
        }
        boolean sexIs = "0".equals(sex) || "1".equals(sex);
        if (!sexIs) {
            return getFailMsg(MessageEnum.SEX_PARAMETER_ERROR.getCode(), MessageEnum.SEX_PARAMETER_ERROR.getMsg(), null);
        }
        userData.setSex(sex);

        //邮箱    格式****@**.com
        String eMail = registerUser.getEMail();
        if (StringUtils.isNotEmpty(eMail)){
            if (isEmail(eMail)){
                userData.setEMail(eMail);
            }else {
                return getFailMsg(MessageEnum.EMAIL_ERROR.getCode(), MessageEnum.EMAIL_ERROR.getMsg(), null);
            }
        }

        userData.setCreateDatetime(getAtPresent());
        userData.setPhoto(registerUser.getPhoto());
        userData.setAge(registerUser.getAge());
        userData.setDes(registerUser.getDes());
        userData.setUserGrade(registerUser.getUserGrade());
        return getSuccessMsg("00","参数验证通过",userData);
    }

    /**
     * 修改用户信息参数校验
     * @param updateUserParam
     * @return
     */
    public static ResponseMsg<?> checkUpdateUser(UpdateUserParam updateUserParam) {
        UpdateUserParam checkUpdateUserParam = new UpdateUserParam();
        //校验id
        if (updateUserParam.getAccount() == null){
            return getFailMsg(MessageEnum.ID_ISNULL.getCode(), MessageEnum.ID_ISNULL.getMsg(), null);
        }
        checkUpdateUserParam.setAccount(updateUserParam.getAccount());
        //检验用户名
        String userName = updateUserParam.getUserName();
        if (StringUtils.isBlank(userName)
                || userName.length() > 50) {
            return getFailMsg(MessageEnum.USERNAME_ISNULL.getCode(), MessageEnum.USERNAME_ISNULL.getMsg(), null);
        }
        checkUpdateUserParam.setUserName(userName);

        //密码   长度不能超过30 不为空 最少6位 不能为汉字 可携带符号
        String passWord = updateUserParam.getPassWord();
        if (StringUtils.isBlank(passWord) || passWord.length() < 6 || passWord.length() > 30) {
            return getFailMsg(MessageEnum.PASSWORD_ERROR.getCode(), MessageEnum.PASSWORD_ERROR.getMsg(), null);
        } else {
            Map<String, Object> checkPasswordMap = checkPassword(passWord);
            //密码加密 加盐
            passWord = DigestUtils.md5Hex(passWord + slat);
            checkUpdateUserParam.setPassWord(passWord);
            checkUpdateUserParam.setPassWordSecurity(String.valueOf(checkPasswordMap.get(Dict.MESSAGE)));
        }

        //地址
        String address = updateUserParam.getAddress();
        if (StringUtils.isNotEmpty(address)) {
            checkUpdateUserParam.setAddress(address);
        }

        //手机号 长度11位
        String phone = updateUserParam.getPhone();
        if (StringUtils.isBlank(phone)) {
            return getFailMsg(MessageEnum.PHONE_ERROR.getCode(), MessageEnum.PHONE_ERROR.getMsg(), null);
        }

        //正则校验格式
        if (!isMobileNO(phone)) {
            return getFailMsg(MessageEnum.PHONE_CODE_ERROR.getCode(), MessageEnum.PHONE_CODE_ERROR.getMsg(), null);
        } else {
            checkUpdateUserParam.setPhone(phone);
        }

        //性别    女生为0 男生为1
        String sex = updateUserParam.getSex();
        if (StringUtils.isBlank(sex)) {
            return getFailMsg(MessageEnum.SEX_ISNULL_ERROR.getCode(), MessageEnum.SEX_ISNULL_ERROR.getMsg(), null);
        }
        boolean sexIs = "0".equals(sex) || "1".equals(sex);
        if (!sexIs) {
            return getFailMsg(MessageEnum.SEX_PARAMETER_ERROR.getCode(), MessageEnum.SEX_PARAMETER_ERROR.getMsg(), null);
        }
        checkUpdateUserParam.setSex(sex);

        //邮箱    格式****@**.com
        String eMail = updateUserParam.getEMail();
        if (StringUtils.isNotEmpty(eMail)){
            if (isEmail(eMail)){
                checkUpdateUserParam.setEMail(eMail);
            }else {
                return getFailMsg(MessageEnum.EMAIL_ERROR.getCode(), MessageEnum.EMAIL_ERROR.getMsg(), null);
            }
        }

        if (StringUtils.isNotBlank(updateUserParam.getHeadPortrait())){
            checkUpdateUserParam.setHeadPortrait(updateUserParam.getHeadPortrait());
        }

        if (StringUtils.isNotBlank(updateUserParam.getDes())){
            checkUpdateUserParam.setDes(updateUserParam.getDes());
        }

        if (StringUtils.isNotBlank(updateUserParam.getAge())){
            checkUpdateUserParam.setAge(updateUserParam.getAge());
        }

        return getFailMsg(MessageEnum.UPDATE_USER_SUCCESS.getCode(), MessageEnum.UPDATE_USER_SUCCESS.getMsg(), checkUpdateUserParam);
    }

    /**
     * 校验密码
     *
     * @param passwordStr
     * @return
     */
    private static Map<String, Object> checkPassword(String passwordStr) {
        Map<String, Object> userMap = new HashMap<>();
        if ((passwordStr.length() < 6 || passwordStr.length() > 30)) {
            userMap.put(Dict.MESSAGE, "密码为 6-30 位字母、数字或英文字符!");
        }
        // Z = 字母       S = 数字           T = 特殊字符
        String regexZ = "[A-Za-z]+";
        String regexS = "^\\d+$";
        String regexT = "[~!@#$%^&*.]+";
        String regexZT = "[a-zA-Z~!@#$%^&*.]+";
        String regexZS = "[0-9A-Za-z]+";
        String regexST = "[\\d~!@#$%^&*.]*";
        String regexZST = "[\\da-zA-Z~!@#$%^&*.]+";

        if (passwordStr.matches(regexZ)) {
            userMap.put(Dict.MESSAGE, "纯字母，弱");
        }
        if (passwordStr.matches(regexS)) {
            userMap.put(Dict.MESSAGE, "纯数字，弱");
        }
        if (passwordStr.matches(regexT)) {
            userMap.put(Dict.MESSAGE, "纯字符，弱");
        }
        if (passwordStr.matches(regexZT)) {
            userMap.put(Dict.MESSAGE, "字母字符，中");
        }
        if (passwordStr.matches(regexZS)) {
            userMap.put(Dict.MESSAGE, "字母数字，中");
        }
        if (passwordStr.matches(regexST)) {
            userMap.put(Dict.MESSAGE, "数字字符，中");
        }
        if (passwordStr.matches(regexZST)) {
            userMap.put(Dict.MESSAGE, "强");
        }
        return userMap;
    }

    /**
     * 校验手机号
     *
     * @param mobiles
     * @return
     */
    private static boolean isMobileNO(String mobiles) {
        String phone_number_reg = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        Pattern p = Pattern.compile(phone_number_reg);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 校验邮箱
     * @param email
     * @return
     */
    private static boolean isEmail(String email) {
        //正则表达式
        String email_regx = "^\\w+(\\w|[.]\\w+)+@\\w+([.]\\w+){1,3}";
        Pattern p = Pattern.compile(email_regx);
        Matcher m = p.matcher(email);
        return m.matches();
    }

}
